﻿using System.Globalization;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using StockholmsStad.Windows.UI.Facade.PresentationModels;
using StockholmsStad.Windows.UI.Infrastructure;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Controllers
{
    public class ScheduleController : IScheduleController
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        public ScheduleController(IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }

        public virtual void OnTimeGenerated(GenerateTimePresentationModel model)
        {
            var scheduleRegion = regionManager.Regions[RegionNames.ScheduleRegion];
            var existingView = scheduleRegion.GetView(ViewNames.ScheduleView);

            if (existingView != null)
            {
                //var view = existingView as GenerateTimePresentationModel;
                scheduleRegion.Activate(existingView);
            }
            else
            {
                //var monthsPresenter = container.Resolve<IGeneratedMonthsPresenter>();
                //monthsPresenter.SetSchedule(model);

                ////IEmployeesDetailsPresenter detailsPresenter = this.container.Resolve<IEmployeesDetailsPresenter>();
                ////detailsPresenter.SetSelectedEmployee(employee);

                //var detailsRegionManager = scheduleRegion.Add(monthsPresenter.View, ViewNames.ScheduleView, true);
                ////var region = detailsRegionManager.Regions[RegionNames.s];
                ////region.Add(monthsPresenter.View, "CurrentProjectsView");
                //scheduleRegion.Activate(monthsPresenter.View);
            }
            //    //var existingModel = view.Model;

            //    /// Det verkar finnas en bugg i region-hanteringen som tvingar fram en koll om huruvida den 
            //    /// existerande vyns id stämmer överens med den efterfrågade (vilket GetView borde ha grejat)
            //    if (existingModel.TicketId == model.TicketId)
            //    {
            //        // vyn existerar och är densamma
            //        detailsRegion.Activate(existingView);
            //    }
            //    else
            //    {
            //        // Vyn existerar, men är inte densamma
            //        AddNewRegion(model, detailsRegion, existingView);
            //    }
            //}
            //else
            //{
            //    AddNewRegion(model, detailsRegion, existingView);
            //}
        }

        private void AddNewRegion(CoupleDetailsPresentationModel model, IRegion detailsRegion, object existingView)
        {
            //if(existingView != null)
            //{
            //    // remove previous region
            //    detailsRegion.Remove(existingView);
            //}

            //var detailsPresenter = container.Resolve<ICouplesDetailsPresenter>();
            //detailsPresenter.SetSelectedCouple(model);

            //detailsRegion.Add(detailsPresenter.View, model.TicketId.ToString(), true);
            //detailsRegion.Activate(detailsPresenter.View);
        }
    }
}